import Vue from "vue";
import Vuex from "vuex";
import http from "@/utils/request";
import { getToken } from "@/utils/authToken";
Vue.use(Vuex);

// 创建一个vuex状态管理器
var store = new Vuex.Store({
    state:{ //调用方案一：this.$store.state.siteTitle
        siteTitle:"健康体检系统管理后台", //站点名称
        adminId:"",
        adminName:"",
        roleName:"" ,
        departName:"",
        adminAccount:"",
        adminPwd:"",
        roleId:"",
    },
    getters:{
        getSiteTitle(state){  //调用方案二：this.$store.getters.getSiteTitle 
            return state.siteTitle
        }
    },
    mutations:{
        setSiteTitle(state,val){
            state.siteTitle = val
        },
        setAdminId(state,val){
            state.adminId = val
        },
        setAdminName(state,val){
            state.adminName = val
        },
        roleName(state,val){
            state.roleName = val
        },
        departName(state,val){
            state.departName = val
        },
        adminAccount(state,val){
            state.adminAccount = val
        },
        adminPwd(state,val){
            state.adminPwd = val
        },
        roleId(state,val){
            state.roleId = val
        }
        

    },
    actions:{
        setAdmin(store,val){
            store.commit("setAdmin",val)
        },
        async getAdminInfo(store){
            return await http.get(
                "/admin/getAdminInfo",
                {
                    params:{
                        token:getToken()
                    }
                }
            ).then(res =>{
                console.log("信息",res)
                if(res != false){
                    // 等会这就去修改state里面的adminId等信息
                    store.commit("setAdminId",res.data.adminInfo.adminId)
                    store.commit("setAdminName",res.data.adminInfo.adminName)
                    store.commit("adminAccount",res.data.adminInfo.adminAccount)
                    store.commit("roleName",res.data.adminInfo.role.roleName)
                    store.commit("adminPwd",res.data.adminInfo.adminPwd)
                    store.commit("departName",res.data.adminInfo.department.departmentName)
                    store.commit("roleId",res.data.adminInfo.role.roleId)
                    return true
                }else{
                    return false
                }
                
            })
        }
    }
})
export default store